import requests
from bs4 import BeautifulSoup
# import csv
import pandas as pd
import datetime


url ="https://top.baidu.com/board?tab=realtime"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')

# 找到热搜榜的标题
title = soup.select(".c-single-text-ellipsis")
titles = []
for i in title:
    titles.append(i.get_text().strip().replace("#"," "))
# print(titles)
# 找到热搜榜的热度
hot = soup.select(".hot-index_1Bl1a")
hots = []
for i in hot:
    hots.append(i.get_text().strip())
# print(hots)
content = list(zip(titles, hots))
# print(content)

# 保存到csv文件
# with open('百度热搜榜.csv', 'w', newline='', encoding='utf-8') as f:
#     writer = csv.writer(f)
#     writer.writerow(['标题', '热度'])
#     writer.writerows(content)


today = datetime.date.today()

# 保存到excel文件
df = pd.DataFrame(content, columns=['标题', '热度'])
df.to_csv(f'百度热搜榜{today}.csv', index=False)